package com.linkoog.devtools.utils;

import com.linkoog.devtools.type.impl.LiteralTypes;
import com.linkoog.devtools.type.impl.ObjectType;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ClassFieldsUtils {
    private static final Map<Class<?>, List<Field>> classFields = new ConcurrentHashMap<>(32);

    public static List<Field> getAllFields(Class clazz){
        return classFields.computeIfAbsent(clazz, clzz -> {
            List<Field> result = new ArrayList<>();
            getAllFields(clazz, result);
            return result;
        });
    }

    private static void getAllFields(Class clazz, List<Field> result){
        if (clazz.getName().equals("java.lang.Object")  || clazz.getName().equals("java.lang.Enum")){
            return;
        }
        getAllFields(clazz.getSuperclass(), result);
        result.addAll(Arrays.asList(clazz.getDeclaredFields()));
    }

    public static void main(String[] args) {
        ObjectType objectType = new ObjectType("111", "111", "111", "1111");

        List<Field> list = getAllFields(LiteralTypes.BooleanPrimitiveBoxType.getClass());
        for (Field field : list) {
            System.out.println(field.getName());
        }
    }
}
